package gases.as3.librarys.components.ui.module
{
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	import flash.utils.setTimeout;
	
	import gases.as3.librarys.components.ui.GUIComponents;
	/**
	 *组件管理池 
	 * @author helloworld
	 * 
	 */	
	public class PanelPoolManage extends GUIComponents
	{
		private  var _objectBuffer:Dictionary;
		private  var _objectIdContain:Dictionary;
		private  var _objectNameContain:Dictionary;
		private	 var _canAdd:Boolean = true;
		private var _childNumber:int;
		
		public function PanelPoolManage()
		{
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			_objectBuffer = new Dictionary();
			_objectIdContain = new Dictionary();
			_objectNameContain = new Dictionary();
		}
		
		
		public function pushDisplayObject(displayObject:GUIComponents):void
		{
			var id:String = displayObject.id;
			var name:String = displayObject.componentName;
			if(!id)
			{
				this.addChild(displayObject);
				return;
			}
			_objectBuffer[id] = displayObject;
			_objectIdContain[id] = displayObject;
			_objectNameContain[name] = displayObject;
			checkPool();
		}
		/**
		 *通过id移除组件 
		 * @param id
		 * 
		 */		
		public function removeDisplayObjectById(id:String):void
		{
			var dis:DisplayObject = _objectIdContain[id] as DisplayObject;
			if(dis)
			{
				if(this.contains(dis))
				{
					delete _objectIdContain[id];
					this.removeChild(dis);
				}
			}
		}
		/**
		 *通过名字移除组件 
		 * @param name
		 * 
		 */		
		public function removeDisplayObjectByName(name:String):void
		{
			var dis:DisplayObject = _objectNameContain[name] as DisplayObject;
			if(dis)
			{
				if(this.contains(dis))
				{
					delete _objectNameContain[name];
					this.removeChild(dis);
				}
			}
		}
		
		/**
		 *通过id获得面板里面的组件 
		 * @param id
		 * @return 
		 * 
		 */		
		public function getDisplayObjectById(id:String):DisplayObject
		{
			var display:DisplayObject = _objectIdContain[id];
			return display;
		}
		
		
		/**
		 *通过name获得面板里面的组件 
		 * @param id
		 * @return 
		 * 
		 */		
		public function getDisplayObjectByName(name:String):DisplayObject
		{
			var display:DisplayObject = _objectNameContain[name];
			return display;
		}
		
		/**
		 *获得子类个数 
		 * @return 
		 * 
		 */		
		public function getChildNumber():int
		{
			return _childNumber;
		}
		
		private function checkPool():void
		{
			if(!_canAdd)
			{
				return;
			}
			for (var key:String in _objectBuffer)
			{
				_canAdd = false;
				setTimeout(addToPanel,20,_objectBuffer[key]);
				delete _objectBuffer[key];
				return;
			}
		}
		
		private function addToPanel(dis:GUIComponents):void
		{
			this.addChild(dis);
			_childNumber++;
			_canAdd = true;
			checkPool();
		}
		
		override public function dispose():void
		{
			for (var key1:String in _objectBuffer)
			{
				delete _objectBuffer[key1];
				_childNumber --;
			}
			for (var key2:String in _objectIdContain)
			{
				delete _objectIdContain[key2];
				_childNumber --;
			}
			for (var key3:String in _objectNameContain)
			{
				delete _objectNameContain[key3];
				_childNumber --;
			}
			super.dispose();
		}
		
	}
}